package com.google.project.server.entity;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.Element;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.listener.StoreCallback;

import com.google.project.server.PMF;
import com.google.project.shared.CryptString;
import com.google.project.shared.ProjectDTO;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Project implements StoreCallback {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String projectId;
	
	@Persistent
	private String projectCode;

	@Persistent
	private String projectName;

	@Persistent
	private Long startDate;

	@Persistent
	private Long finishDate;

	@Persistent
	private Long dueDate;

	@Persistent
	private String accountName;
	@Persistent
	private String userId;
	@Persistent
	private int status;

	@Persistent
	private String location;

	@Persistent
	private String consultant;

	@Persistent
	private String phoneConsultant;

	@Persistent
	private String faxConsultant;

	@Persistent
	private String investor;

	@Persistent
	private String phoneInvestor;

	@Persistent
	private String faxInvestor;

	@Persistent
	private String description;
	
	@Persistent
	private int  numOfEmpReq;
	@Persistent
	private int  numOfEmpRep;
	@Persistent
	private int numOfOPT;
	@Persistent
	private int numOfOPTRep;
	@Persistent
	private int isReq;
	
	@Persistent(mappedBy = "project")
	@Element(dependent = "true")
	private List<ProjectTask> listOfTask = new ArrayList<ProjectTask>();

	@Persistent(mappedBy = "project")
	@Element(dependent = "true")
	private List<SubConstructor> listOfSubContructor = new ArrayList<SubConstructor>();

	@Persistent(mappedBy = "project")
	@Element(dependent = "true")
	private List<ProjectStaff> listOfStaff = new ArrayList<ProjectStaff>();

	@Persistent(mappedBy = "project")
	@Element(dependent = "true")
	private List<DailyReport> listOfdailyReport=new ArrayList<DailyReport>();
	
	@Persistent(mappedBy = "project")
	@Element(dependent = "true")
	private List<ProjectReport> listOfProjectReport=new ArrayList<ProjectReport>();

	@Persistent(mappedBy = "project")
	@Element(dependent = "true")
	private List<Message> listOfMessage=new ArrayList<Message>();
	public Project() {
	}
	   CryptString crypt=new CryptString();
	   
	public Project(ProjectDTO projectDTO) {
		this.projectCode = projectDTO.getProjectCode();
//		try {
//			this.projectName =crypt.encryptBase64( projectDTO.getProjectName());
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		this.projectName =projectDTO.getProjectName();
		this.startDate = projectDTO.getStartDate();
		this.finishDate = projectDTO.getFinishDate();
		this.dueDate = projectDTO.getDueDate();
		this.accountName = projectDTO.getAccountName();
		this.userId=projectDTO.getUserId();
		this.status = projectDTO.getStatus();
		this.location = projectDTO.getLocation();
		this.consultant = projectDTO.getConsultant();
		this.phoneConsultant = projectDTO.getPhoneConsultant();
		this.faxConsultant = projectDTO.getFaxConsultant();
		this.investor = projectDTO.getInvestor();
		this.phoneInvestor = projectDTO.getPhoneInvestor();
		this.faxInvestor = projectDTO.getFaxInvestor();
		this.description = projectDTO.getDescription();
		this.numOfEmpRep=projectDTO.getNumOfEmpRep();
		this.numOfEmpReq=projectDTO.getNumOfEmpReq();
		this.numOfOPT=projectDTO.getNumOfOPT();
		this.numOfOPTRep=projectDTO.getNumOfOPTRep();
		this.isReq=projectDTO.getIsReq();
		
	}

	public ProjectDTO toDTO() {
		ProjectDTO data = new ProjectDTO();
		data.setProjectID(projectId);
		data.setProjectCode(projectCode);
//		try {
//			data.setProjectName(crypt.decryptBase64( projectName));
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		data.setProjectName(projectName);
		data.setFinishDate(finishDate);
		data.setStartDate(startDate);
		data.setDueDate(dueDate);
		data.setStatus(status);
		data.setAccountName(accountName);
		data.setLocation(location);
		data.setConsultant(consultant);
		data.setPhoneConsultant(phoneConsultant);
		data.setFaxConsultant(faxConsultant);
		data.setInvestor(investor);
		data.setPhoneInvestor(phoneInvestor);
		data.setFaxInvestor(faxInvestor);
		data.setDescription(description);
		data.setUserId(userId);
		data.setNumOfEmpRep(numOfEmpRep);
		data.setNumOfEmpReq(numOfEmpReq);
		data.setNumOfOPT(numOfOPT);
		data.setNumOfOPTRep(numOfOPTRep);
		data.setIsReq(isReq);
		
		Double complete =0d;
		List<ProjectTask> listTask  = new ArrayList<ProjectTask>();
		listTask=this.getListOfTask();
		int i = listTask.size();
		for(ProjectTask task: listOfTask)
		{
			if(task.getStatus()==3)
			{
				continue;
			}
			if(task.getParentId()==0&&task.getIsMile()==0)
			{
				Double comOfProject = (task.getFinishedWork()/task.getAmountOfWork())*(task.getWeight()/100);
				complete = complete+comOfProject;
			}
		}
		complete = complete*10000;
		 complete = complete.intValue()+0d;
		 complete = complete/100;
		data.setComplete(complete);
		return data;
	}

	public ProjectDTO toDTOFullData() {
		ProjectDTO data = new ProjectDTO();
		data.setProjectID(projectId);
		data.setProjectCode(projectCode);
//		try {
//			data.setProjectName(crypt.decryptBase64( projectName));
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		data.setProjectName(projectName);
		data.setFinishDate(finishDate);
		data.setStartDate(startDate);
		data.setDueDate(dueDate);
		data.setStatus(status);
		data.setAccountName(accountName);
		data.setUserId(userId);
		data.setLocation(location);
		data.setConsultant(consultant);
		data.setPhoneConsultant(phoneConsultant);
		data.setFaxConsultant(faxConsultant);
		data.setInvestor(investor);
		data.setPhoneInvestor(phoneInvestor);
		data.setFaxInvestor(faxInvestor);
		data.setDescription(description);
		if (listOfSubContructor != null && listOfSubContructor.size() > 0) {
			for (int i = 0; i < listOfSubContructor.size(); i++) {
				data.getListOfSubContructor().add(
						listOfSubContructor.get(i).toDTO());
			}
		}
		data.setNumOfEmpRep(numOfEmpRep);
		data.setNumOfEmpReq(numOfEmpReq);
		data.setNumOfOPT(numOfOPT);
		data.setNumOfOPTRep(numOfOPTRep);
		data.setIsReq(isReq);
		return data;
	}

	public void updateFromDTO(ProjectDTO projectDTO) {
		this.projectCode = projectDTO.getProjectCode();
//		try {
//			this.projectName =crypt.encryptBase64( projectDTO.getProjectName());
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		this.projectName =projectDTO.getProjectName();
		this.startDate = projectDTO.getStartDate();
		this.finishDate = projectDTO.getFinishDate();
		this.dueDate = projectDTO.getDueDate();
		this.accountName = projectDTO.getAccountName();
		this.userId=projectDTO.getUserId();
		this.status = projectDTO.getStatus();
		this.location = projectDTO.getLocation();
		this.consultant = projectDTO.getConsultant();
		this.phoneConsultant = projectDTO.getPhoneConsultant();
		this.faxConsultant = projectDTO.getFaxConsultant();
		this.investor = projectDTO.getInvestor();
		this.phoneInvestor = projectDTO.getPhoneInvestor();
		this.faxInvestor = projectDTO.getFaxInvestor();
		this.description = projectDTO.getDescription();
		this.numOfEmpRep=projectDTO.getNumOfEmpRep();
		this.numOfEmpReq=projectDTO.getNumOfEmpReq();
		this.numOfOPT=projectDTO.getNumOfOPT();
		this.numOfOPTRep=projectDTO.getNumOfOPTRep();
		this.isReq=projectDTO.getIsReq();
	}

	public List<ProjectStaff> getListOfStaff() {
		return listOfStaff;
	}

	public void setListOfStaff(List<ProjectStaff> listOfStaff) {
		this.listOfStaff = listOfStaff;
	}

	public List<SubConstructor> getListOfSubContructor() {
		return listOfSubContructor;
	}

	public void setListOfSubContructor(List<SubConstructor> listOfSubContructor) {
		this.listOfSubContructor = listOfSubContructor;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getConsultant() {
		return consultant;
	}

	public void setConsultant(String consultant) {
		this.consultant = consultant;
	}

	public String getPhoneConsultant() {
		return phoneConsultant;
	}

	public void setPhoneConsultant(String phoneConsultant) {
		this.phoneConsultant = phoneConsultant;
	}

	public String getFaxConsultant() {
		return faxConsultant;
	}

	public void setFaxConsultant(String faxConsultant) {
		this.faxConsultant = faxConsultant;
	}

	public String getInvestor() {
		return investor;
	}

	public void setInvestor(String investor) {
		this.investor = investor;
	}

	public String getPhoneInvestor() {
		return phoneInvestor;
	}

	public void setPhoneInvestor(String phoneInvestor) {
		this.phoneInvestor = phoneInvestor;
	}

	public String getFaxInvestor() {
		return faxInvestor;
	}

	public void setFaxInvestor(String faxInvestor) {
		this.faxInvestor = faxInvestor;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	
	@Override
	public void jdoPreStore() {
		// TODO Auto-generated method stub

	}

	public String getProjectCode() {
		return projectCode;
	}

	public void setProjectCode(String projectCode) {
		this.projectCode = projectCode;
	}

	public String getProjectName() {
//		String _projectName = null;
//	try {
//		_projectName=crypt.decryptBase64(projectName);
//	} catch (Exception e) {
//		// TODO Auto-generated catch block
//		e.printStackTrace();
//	}
		return  projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public Long getStartDate() {
		return startDate;
	}

	public void setStartDate(Long startDate) {
		this.startDate = startDate;
	}

	public Long getFinishDate() {
		return finishDate;
	}

	public void setFinishDate(Long finishDate) {
		this.finishDate = finishDate;
	}

	public Long getDueDate() {
		return dueDate;
	}

	public void setDueDate(Long dueDate) {
		this.dueDate = dueDate;
	}

	public String getProjectId() {
		return projectId;
	}

	public void getDetails() {

	}

	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public void addTask(ProjectTask task) {
		this.listOfTask.add(task);
	}

	public List<ProjectTask> getListOfTask() {
		return listOfTask;
	}

	public void setListOfTask(List<ProjectTask> listOfTask) {
		this.listOfTask = listOfTask;
	}

	public static Project getProject(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Project oneResult = null, detached = null;
		Query q = pm.newQuery(Project.class, "projectId == :id");
		q.setUnique(true);
		try {
			oneResult = (Project) q.execute(id);
			if (oneResult != null) {
				// fetch friends list before detaching
				oneResult.getListOfTask();
				detached = pm.detachCopy(oneResult);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
			q.closeAll();
		}
		return detached;
	}

	public List<DailyReport> getListOfdailyReport() {
		return listOfdailyReport;
	}

	public void setListOfdailyReport(List<DailyReport> listOfdailyReport) {
		this.listOfdailyReport = listOfdailyReport;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public List<Message> getListOfMessage() {
		return listOfMessage;
	}

	public void setListOfMessage(List<Message> listOfMessage) {
		this.listOfMessage = listOfMessage;
	}

	public List<ProjectReport> getListOfProjectReport() {
		return listOfProjectReport;
	}

	public void setListOfProjectReport(List<ProjectReport> listOfProjectReport) {
		this.listOfProjectReport = listOfProjectReport;
	}

	public int getNumOfEmpReq() {
		return numOfEmpReq;
	}

	public void setNumOfEmpReq(int numOfEmpReq) {
		this.numOfEmpReq = numOfEmpReq;
	}

	public int getNumOfEmpRep() {
		return numOfEmpRep;
	}

	public void setNumOfEmpRep(int numOfEmpRep) {
		this.numOfEmpRep = numOfEmpRep;
	}

	public int getIsReq() {
		return isReq;
	}

	public void setIsReq(int isReq) {
		this.isReq = isReq;
	}

	public int getNumOfOPT() {
		return numOfOPT;
	}

	public void setNumOfOPT(int numOfOPT) {
		this.numOfOPT = numOfOPT;
	}

	public int getNumOfOPTRep() {
		return numOfOPTRep;
	}

	public void setNumOfOPTRep(int numOfOPTRep) {
		this.numOfOPTRep = numOfOPTRep;
	}
	
	
	
	
}
